package com.vvv.method;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author vvv
 * @date 2023-10-19 16 01
 * discription
 */
public class ABAProblem {
    static AtomicReference<String> res = new AtomicReference<>("A");
    public static void main(String[] args) throws InterruptedException {
        String preStr = res.get();
        other();
        Thread.sleep(1000);
        System.out.println("main线程 a --> c " + res.compareAndSet(preStr, "C"));
    }

    public static void other() throws InterruptedException {
        new Thread(() -> {
            Thread thread = Thread.currentThread();
            String preStr = res.get();
            System.out.println(thread.getName() + "线程 a --> b " + res.compareAndSet(preStr, "B"));
        }, "t1").start();
        Thread.sleep(500);
        new Thread(() -> {
            Thread thread = Thread.currentThread();
            String preStr = res.get();
            System.out.println(thread.getName() + "线程 b --> a " + res.compareAndSet(preStr, "A"));
        }, "t2").start();
    }
}
